import MySQLdb
import re
import sys

db = MySQLdb.connect(host="localhost", user="jimmy", passwd="Ahbh37-4ta", db="twitter")

cur = db.cursor() 

fd = open(sys.argv[1], "r")

line = fd.readline().strip()
line = fd.readline().strip()

tweet = {} 
r = re.compile('\s+')
r2 = re.compile("\/")

while line:
	line = line.strip()
	if len(line) > 0:
		t, v = r.split(line, 1)
		
		tweet[t] = v

		if t == 'W':
			usplit = r2.split(tweet['U'])
			if len(usplit) == 4:
				tweet['U'] = usplit[3]
			else:
				tweet['U'] = ""
			cur.execute("INSERT INTO tweet (time, screen_name, text) VALUES (%s,%s,%s)", (tweet['T'], tweet['U'], tweet['W']))
			tweet = {} 
	line = fd.readline()

fd.close()
cur.close()
db.close()
